#!/usr/bin/env python
# USP Calendar Plugin Version 1.0
##Description:Calendar only (no reminders)
import gtk
import gtk.glade
import sys
import os
import pango
import gobject
from datetime import datetime
import gconf
from execute import Execute
from easygconf import SetGconf
from easygconf import WriteGconf


class pluginclass:
	TARGET_TYPE_TEXT = 80
	toButton = [ ( "text/uri-list", 0, TARGET_TYPE_TEXT ) ]
	"""This is the main class for the plugin"""
	"""It MUST be named pluginclass"""

	def __init__(self, USPWin):
		self.USPWin = USPWin

		#The Glade file for the plugin
		self.gladefile = os.path.join(os.path.dirname(__file__), "uspcalendar.glade")

		#Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"window1")

		#These properties are NECESSARY to maintain consistency
		#throughout USP

		#Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("window1")

		#Set 'heading' property for plugin
		self.heading = "Calendar"

		#This should be the first item added to the window in glade
		self.content_holder = self.wTree.get_widget("eventbox1")
		self.cal=self.wTree.get_widget("calendar1")

		#Specify plugin width
		self.width = 150

		#Plugin icon
		self.icon = 'gnome-calendar.png'

		# Gconf stuff
		self.gconf_dir = '/apps/usp/plugins/calendar'
		self.client = gconf.client_get_default()
		self.client.add_dir( '/apps/usp/plugins/calendar', gconf.CLIENT_PRELOAD_NONE )
		self.client.notify_add('/apps/usp/plugins/calendar/sticky',self.RegenPlugin)
		self.client.notify_add('/apps/usp/plugins/calendar/icon',self.RegenPlugin)

		self.RegenPlugin()
		#Connect event handlers
		dic = {	"on_window1_destroy" : gtk.main_quit,
				"on_calendar1_day_selected_double_click" : self.runcal}
		self.wTree.signal_autoconnect(dic)

	def RegenPlugin(self, *args, **kargs):
		self.GetGconfEntries()

	def GetGconfEntries(self):
		self.client = gconf.client_get_default()
		self.sticky = SetGconf( self.client, "bool", "/apps/usp/plugins/calendar/sticky", False )
		self.minimized = SetGconf( self.client, "bool", "/apps/usp/plugins/calendar/minimized", False )
		self.icon = SetGconf( self.client, "string", "/apps/usp/plugins/calendar/icon", "gnome-calendar.png" )
		self.width = SetGconf( self.client, "int", "/apps/usp/plugins/calendar/width", 200 )

	def SetHidden( self, state ):
		if state == True:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/calendar/minimized", True )
		else:
			WriteGconf( self.client, "bool", "/apps/usp/plugins/calendar/minimized", False )

	def runcal(self, widget):
		clicked_date=self.cal.get_date()
		# FOR SOME REASON MONTH IS ZERO OFFSET(for name arrays probably) SO NEED TO ADD ONE TO MONTH ??????
		evo_date=str(clicked_date[0])+str(clicked_date[1]+1).zfill(2)+str(clicked_date[2]).zfill(2)
		cmd=["evolution","calendar:///?startdate="+evo_date]
		pid = os.fork()
		if pid:
			os.chdir(os.path.expanduser("~"))
			os.spawnvp(os.P_NOWAIT,cmd[0], cmd)
			os._exit(0)
		self.USPWin.wTree.get_widget("window1").hide()
		# Reset Display to Today's Date
		self.cal.select_month(int(datetime.today().strftime("%m"))-1,int(datetime.today().strftime("%Y")))
		self.cal.select_day(int(datetime.today().strftime("%d")))
#---------------------------------------------------------------------------------------#
# USPconfig Section Below
#---------------------------------------------------------------------------------------#
# This is the Config Class it must be called "cfgpluginclass' and the gtk.Window must be called 'config'
class cfgpluginclass:
	def __init__(self):
		# The Gladefile for the plugins USPconfig Tab
		self.gladefile = os.path.join(os.path.dirname(__file__), "uspcalendar.glade")
		# Read GLADE file
		self.wTree = gtk.glade.XML(self.gladefile,"config")
		# Set 'window' property for the plugin (Must be the root widget)
		self.window = self.wTree.get_widget("config")
		# Content Place Holder must be eventbox2
		self.content_holder = self.wTree.get_widget("eventbox2")
		# Set Heading, this will be used for the tab label in USPconfig
		self.heading = "Calendar"
		# GConf Stuff - This just makes sure a gconf path is there.
		self.gconf_dir = '/apps/usp/plugins/calendar'
		self.client = gconf.client_get_default()
		self.client.add_dir('/apps/usp/plugins/calendar', gconf.CLIENT_PRELOAD_NONE)
		# Setup the Functions of the Glade files spin controls or entry boxes for Value Changes
		# Tip: For spinbutton controls use the "_value_changed" event not the "_changed" event.
		dic = { "on_window2_destroy" : gtk.main_quit,
				"on_CalHSpin_changed" : self.height_spin,
				"on_CalWSpin_changed" : self.width_spin,
				"on_CalTasksHSpin_changed" : self.tasks_h_spin,
				"on_CalAppsHSpin_changed" : self.apps_h_spin,
				"on_CalHeadSizeSpin_changed" : self.head_spin,
				"on_CalItemSizeSpin_changed" : self.item_spin,
				"on_CalIconEntry_changed" : self.icon_changed,
				"on_CalShowChkBtn_toggled" : self.show_t_a,
				"on_StickyChkBtn_toggled" : self.sticky}
		self.wTree.signal_autoconnect(dic)
		# Read Values from GConf for USPconfig Tab and Set Default Values in GConf if they don't exist.
		self.wTree.get_widget("StickyChkBtn").set_active(SetGconf(self.client,'bool','/apps/usp/plugins/calendar/sticky',False))
		self.wTree.get_widget("CalIconEntry").set_text(SetGconf( self.client, "string", '/apps/usp/plugins/calendar/icon', "gnome-calendar.png"))
		self.wTree.get_widget("CalHSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/height',200))
		self.wTree.get_widget("CalWSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/width',250))
		self.wTree.get_widget("CalTasksHSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/height_tasks',80))
		self.wTree.get_widget("CalAppsHSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/height_appoints',80))
		self.wTree.get_widget("CalHeadSizeSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/heading_font_size',10))
		self.wTree.get_widget("CalItemSizeSpin").set_value(SetGconf(self.client,'int','/apps/usp/plugins/calendar/item_font_size',8))
		self.wTree.get_widget("CalShowChkBtn").set_active(SetGconf(self.client,'bool','/apps/usp/plugins/calendar/show_tasks_appoints',False))

	# Functions for Changing Values when items checked or altered.
	def sticky(self, *args, **kargs):
		if self.wTree.get_widget("StickyChkBtn").get_active() == True:
			self.client.set_bool('/apps/usp/plugins/calendar/sticky',True)
		else:
			self.client.set_bool('/apps/usp/plugins/calendar/sticky',False)

	def icon_changed(self, *args, **kargs):
		self.client.set_string('/apps/usp/plugins/calendar/icon',self.wTree.get_widget("CalIconEntry").get_text())

	def height_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/height',int(self.wTree.get_widget("CalHSpin").get_value()))

	def width_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/width',int(self.wTree.get_widget("CalWSpin").get_value()))

	def tasks_h_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/height_tasks',int(self.wTree.get_widget("CalTasksHSpin").get_value()))

	def apps_h_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/height_appoints',int(self.wTree.get_widget("CalAppsHSpin").get_value()))

	def head_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/heading_font_size',int(self.wTree.get_widget("CalHeadSizeSpin").get_value()))

	def item_spin(self, *args, **kargs):
		self.client.set_int('/apps/usp/plugins/calendar/item_font_size',int(self.wTree.get_widget("CalItemSizeSpin").get_value()))

	def show_t_a(self, *args, **kargs):
		if self.wTree.get_widget("CalShowChkBtn").get_active() == True:
			self.client.set_bool('/apps/usp/plugins/calendar/show_tasks_appoints',True)
		else:
			self.client.set_bool('/apps/usp/plugins/calendar/show_tasks_appoints',False)

